package edu.ynu.se.xiecheng.achitectureclass.common.controller;

import edu.ynu.se.xiecheng.achitectureclass.common.dao.CameraRecodeDAO;
import edu.ynu.se.xiecheng.achitectureclass.common.entity.CommonRecode;
import edu.ynu.se.xiecheng.achitectureclass.common.service.CameraRecodeService;
import edu.ynu.se.xiecheng.achitectureclass.common.service.DataFetcherService;
import edu.ynu.se.xiecheng.achitectureclass.common.service.HistoryService;
import edu.ynu.se.xiecheng.achitectureclass.common.utils.TokenManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/data")
public class DataController {

    @Autowired
    private DataFetcherService dataFetcherService;

    @Autowired
    private TokenManager tokenManager;

    @Autowired
    private CameraRecodeDAO cameraRecodeDAO;

    @Autowired
    private CameraRecodeService cameraRecodeService;

    @Autowired
    private HistoryService historyService;

    @GetMapping("/common-device")
    public HashMap<String, CommonRecode> getDeviceData() {
        return dataFetcherService.getDeviceData();
    }

    @GetMapping("/camera-device")
    public HashMap<String, String> getCameraData(
            @RequestParam String deviceSerial,
            @RequestParam String channelNo,
            @RequestParam String quality
    ) throws IOException {
        String accessToken = tokenManager.getCameraToken();
        System.out.println(accessToken);
        return cameraRecodeService.ObtainDataFromServer(accessToken, deviceSerial, channelNo, quality);
    }

    @GetMapping("/common-history")
    public List<CommonRecode> getHistoryData(
            @RequestParam String seriesId
    ) {
        for (CommonRecode commonRecode: historyService.getHistoryData(seriesId)){
            System.out.println(commonRecode.toString());
        }
        return historyService.getHistoryData(seriesId);
    }
}
